import 'package:class_teacher_student/model/Letter.dart';
import 'package:class_teacher_student/model/section.dart';
import 'package:class_teacher_student/model/student.dart';
import 'package:flutter/material.dart';

extension ListSectionExtension on List<Section>{

  List<Section> mergeAlphabet(){

    List<Section> newSections = List<Section>();
    int i=0;
    for(Letter letter in Letter.getLetters()){
      Section letterSection = Section();
      letterSection.isLetter=true;
      letterSection.letter = letter.character;
      letterSection.key = GlobalKey();
      newSections.add(letterSection);
      letter.indexOfSection=i;
      i++;
      for(Section section in this){
        if(section.letter == letter.character){
          newSections.add(section);
          i++;
        }
      }
    }

    return newSections;
  }

  double getScrollOffsetByAlphabetIndex(int indexOfAlphabet){
    Letter letter= Letter.getLetters()[indexOfAlphabet];
    int indexOfSection = letter.indexOfSection;
    //计算所有section距离列表顶部的偏移量
    return calculteOffset(indexOfSection);
  }

  double calculteOffset(int endIndex){
    double hNormal = Section.heightOfNormal;
    double hLetter = Section.heightOfLetter;
    double offset=0;
    int realEndIndex=endIndex>this.length-1 ? this.length-1 : endIndex;

    for(var i=0; i<realEndIndex; i++) {
      Section section = this[i];
      section.offsetY=offset;

      if(section.isLetter)
        offset +=hLetter;
      else {
        if (section.isExpanded) {
          offset += hNormal +
              section.students.length * Student.height;
        } else {
          offset += hNormal;
        }
      }
    }

    return offset;
  }

}